﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ERP.Manager.BusinessServices;
using ERP.Manager.DataEntity;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class ECS_goods_attrController : Controller
    {
        private readonly ECS_goods_typeinfoService ecs_goods_typeinfoservice;
        private readonly ECS_goods_attrinfoService ecs_goods_attrinfoservice;
        private readonly ECS_attributeinfoService ecs_attributeinfoservice;
        public ECS_goods_attrController() { }
        public ECS_goods_attrController(ECS_goods_typeinfoService ecs_goods_typeinfoservice, ECS_attributeinfoService ecs_attributeinfoservce, ECS_goods_attrinfoService ecs_goods_attrinfoservice)
        {
            this.ecs_goods_typeinfoservice = ecs_goods_typeinfoservice;
            this.ecs_attributeinfoservice = ecs_attributeinfoservce;
            this.ecs_goods_attrinfoservice = ecs_goods_attrinfoservice;
        }
        #region 添加页面
        public ActionResult AddECS_goods_attr(int goods_id, int goods_type_id)
        {
            ViewData["goods_id"] = goods_id;
            ECS_goods_typeinfo ecs_goods_typeinfo = ecs_goods_typeinfoservice.detail_ecs_goods_typeinfo(goods_type_id);
            ViewData["ecs_goods_typeinfo"] = ecs_goods_typeinfo;
            List<ECS_attributeinfo> ecs_attributeinfo = ecs_attributeinfoservice.all_ecs_attributeinfo(goods_type_id);
            ViewData["ecs_attributeinfo"] = ecs_attributeinfo;
            return View();
        }
        #endregion

        #region Ajax添加商品属性
        public string AjaxAddgoods_attr(string goods_id, string goods_attr)
        {
            try
            {
                int rowcount = 0;
                int new_goods_id = int.Parse(goods_id.ToString());
                string[] new_goods_attr = goods_attr.Split(';');
                for (int i = 0; i < new_goods_attr.Length - 1; i++)
                {
                    ECS_goods_attrinfo ecs_goods_attrinfo = new ECS_goods_attrinfo();
                    ecs_goods_attrinfo.ecsgoodsinfo.goods_id = new_goods_id;
                    string[] new_goods_attrs = new_goods_attr[i].Split(',');
                    ecs_goods_attrinfo.ecsattributeinfo.attr_id = int.Parse(new_goods_attrs[0]);
                    ecs_goods_attrinfo.attr_value = new_goods_attrs[1];
                    ecs_goods_attrinfo.attr_price = new_goods_attrs[2] == "" ? 0 : int.Parse(new_goods_attrs[2]);
                    rowcount = ecs_goods_attrinfoservice.add_ecs_goods_attrinfo(ecs_goods_attrinfo);
                }
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 修改商品属性
        public ActionResult UpdateECS_goods_attr(int goods_id, int goods_type_id)
        {
            ViewData["goods_id"] = goods_id;
            //得到商品类型
            ECS_goods_typeinfo ecs_goods_typeinfo = ecs_goods_typeinfoservice.detail_ecs_goods_typeinfo(goods_type_id);
            ViewData["ecs_goods_typeinfo"] = ecs_goods_typeinfo;
            //得到商品类型的属性
            List<ECS_attributeinfo> ecs_attributeinfo = ecs_attributeinfoservice.all_ecs_attributeinfo(goods_type_id);
            ViewData["ecs_attributeinfo"] = ecs_attributeinfo;
            List<ECS_goods_attrinfo> list = ecs_goods_attrinfoservice.all_ecs_goods_attrinfo(goods_id);
            ViewData["ecs_goods_attrinfo"] = list;
            return View();
        }
        #endregion

        #region AJax修改商品属性
        public string AjaxUpdategoods_attr(string goods_id, string goods_attr)
        {
            try
            {
                int rowcount = 0;
                int new_goods_id = int.Parse(goods_id.ToString());
                string[] new_goods_attr = goods_attr.Split(';');
                for (int i = 0; i < new_goods_attr.Length - 1; i++)
                {
                    ECS_goods_attrinfo ecs_goods_attrinfo = new ECS_goods_attrinfo();
                    ecs_goods_attrinfo.ecsgoodsinfo.goods_id = new_goods_id;
                    string[] new_goods_attrs = new_goods_attr[i].Split(',');
                    ECS_goods_attrinfo ecs_goods_attr = ecs_goods_attrinfoservice.all_ecs_goods_attrinfo_attr_id(new_goods_id, int.Parse(new_goods_attrs[0]));
                    if (ecs_goods_attr != null && ecs_goods_attr.ecsattributeinfo.attr_id == int.Parse(new_goods_attrs[0]))
                    {
                        ecs_goods_attrinfo.ecsattributeinfo.attr_id = int.Parse(new_goods_attrs[0]);
                        ecs_goods_attrinfo.attr_value = new_goods_attrs[1];
                        ecs_goods_attrinfo.attr_price = new_goods_attrs[2] == "" ? 0 : decimal.Parse(new_goods_attrs[2]);
                        rowcount = ecs_goods_attrinfoservice.update_ecs_goods_attrinfo(ecs_goods_attrinfo);
                    }
                    else
                    {
                        ecs_goods_attrinfo.ecsattributeinfo.attr_id = int.Parse(new_goods_attrs[0]);
                        ecs_goods_attrinfo.attr_value = new_goods_attrs[1];
                        ecs_goods_attrinfo.attr_price = new_goods_attrs[2] == "" ? 0 : decimal.Parse(new_goods_attrs[2]);
                        rowcount = ecs_goods_attrinfoservice.add_ecs_goods_attrinfo(ecs_goods_attrinfo);
                    }
                }
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }

        }
        #endregion
    }
}